package com.example.demo.service.impl;

import com.example.demo.bean.SysMenu;
import com.example.demo.dao.SystemMenuMapper;
import com.example.demo.service.SystemMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class SystemMenuserviceImpl implements SystemMenuService {
    @Autowired
    SystemMenuMapper systemMenuMapper;

    @Override
    public List<SysMenu> querMenu() {
        List<SysMenu> list = systemMenuMapper.querMenu();
        return sysMenuTree(list);
    }
    private List<SysMenu> sysMenuTree(List<SysMenu> list){
        List<SysMenu> sysMenusTree = new ArrayList<>();
        List<SysMenu> parents = new ArrayList<>();
        for(SysMenu sysMenu:list){
            if(sysMenu.getPid()==0){
                parents.add(sysMenu);
            }
        }
        for(SysMenu parent:parents){
            List<SysMenu> children = new ArrayList<>();
            for(SysMenu sysMenu:list){
                if(parent.getId()==sysMenu.getPid()){
                    children.add(sysMenu);
                }
                parent.setChildren(children);
            }
            sysMenusTree.add(parent);
        }
        return sysMenusTree;
    }

}
